package com.novel.authorization.filter;

import cn.hutool.core.text.AntPathMatcher;
import com.novel.base.config.properties.IgnoreProperties;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * GatewayFilter
 *
 * @author 樊温军
 * @date 2025/5/8 下午4:40
 */
@Slf4j
@Component
@Order(-1)
public class GatewayFilter extends GenericFilterBean {

    @Resource
    private IgnoreProperties ignoreProperties;

    @Override
    @SneakyThrows
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        List<String> ignoreUrls = ignoreProperties.getIgnoreUrls();
        for (String ignoreUrl : ignoreUrls) {
            AntPathMatcher antPathMatcher = new AntPathMatcher();
            if (antPathMatcher.match(ignoreUrl, request.getRequestURI())) {
                chain.doFilter(request, response);
                return;
            }
        }
        chain.doFilter(request, response);
    }
}
